package no.sachmuld.methodcombination.examples.reflection.fragilebaseclass;

import no.sachmuld.methodcombination.api.MethodCombinationSupport;
import no.sachmuld.methodcombination.api.MethodInvocation;
import no.sachmuld.methodcombination.api.reflection.Default;

import java.util.*;

/**
 *
 */
public class MethodCombinationHashSet<E> extends MethodCombinationSupport<HashSetBehaviour<E>> {

    private HashMap<E,Object> map = new HashMap<>();
    private static final Object PRESENT = new Object();

    public MethodCombinationHashSet() {

        with(new HashSetBehaviour<E>() {

            @Override
            public boolean add(E e, MethodInvocation<Boolean> methodInvocation) {
                return map.put(e, PRESENT) == null;
            }

            @Override
            public boolean addAll(Collection<? extends E> c, MethodInvocation<Boolean> methodInvocation) {
                boolean modified = false;
                for (E e : c)
                    //if (MethodCombinationHashSet.this.add(e))
                    if (add(e, methodInvocation))
                        modified = true;
                return modified;
            }

        });
    }

    final public boolean add(E e) {
        return new Default<Boolean>().build(HashSetBehaviour.class, MethodCombinationHashSet.class, "add", Object.class).execute(this, chain(), e);
    }

    final public boolean addAll(Collection<? extends E> c) {
        return new Default<Boolean>().build(HashSetBehaviour.class, MethodCombinationHashSet.class, "addAll", Collection.class).execute(this, chain(), c);
    }
}
